﻿using Ado.Interface;
using Factory;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utility;

namespace Tools.ModelFormat.Win
{
    public partial class frmMain : Form
    {
        #region Identity +   static string ConnStr、IDbHelper helper
        /// <summary>
        /// 连接字符串
        /// </summary>
        private static string ConnStr = "";
        /// <summary>
        /// MSSQL数据库访问服务
        /// </summary>
        private IDbHelper helper = SimpleDbHelperFactory.Create(SimpleDbHelperFactory.DbHelperTypeEnum.MsSql); 
        #endregion
        public frmMain()
        {
            InitializeComponent();
        }

        #region 绑定查询按钮，初始化页面显示+void btnQuery_Click(object sender, EventArgs e
        /// <summary>
        /// 绑定查询按钮，初始化页面显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, EventArgs e)
        {

            this.cboTable.DataSource = GetDbList();
            InitDbList();
        } 
        #endregion

        #region 绑定数据库名下拉列表选择变化事件+void cboTable_SelectedIndexChanged(object sender, EventArgs e)
        /// <summary>
        /// 绑定数据库名下拉列表选择变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitDbList();
        } 
        #endregion

        #region 绑定页码文本框变化事件 +void txtPageIndex_TextChanged(object sender, EventArgs e)
        /// <summary>
        /// 绑定页码文本框变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPageIndex_TextChanged(object sender, EventArgs e)
        {
            InitDbList();
        } 
        #endregion

        #region 内部辅助方法+  Init()、List<string> GetDbList()、PageModel<DbTableModel> GetPager(string dbName, int PageIndex = 1)
        /// <summary>
        /// 初始化datagrid
        /// </summary>
        private void InitDbList()
        {
            string dbName = this.cboTable.SelectedItem.ToString();

            string sPageIndex = this.txtPageIndex.Text.Trim();
            int pageIndex = !string.IsNullOrEmpty(sPageIndex)?Convert.ToInt32(sPageIndex):1;
            var list = GetPager(dbName, pageIndex);
            this.dgDbTable.DataSource = list.Rows;
            this.lbCount.Text = list.PageCount.ToString();
            this.txtPageIndex.Text = list.PageIndex.ToString();
        }

        /// <summary>
        /// 获取数据库名列表
        /// </summary>
        /// <returns></returns>
        private List<string> GetDbList()
        {
            ConnStr = string.Format(StaticConstant.ConnStr, txtServer.Text.Trim(), txtUid.Text.Trim(), txtPwd.Text.Trim());

            List<string> tList = new List<string>();
            SqlDataReader drDb = null;


            helper.ExecuteReader(ConnStr, StaticConstant.DbSql, out drDb, () =>
            {
                tList.Add(drDb[0].ToString());
            });
            return tList;


        }
        /// <summary>
        /// 获取标明分页列表
        /// </summary>
        /// <param name="dbName"></param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        private PageModel<DbTableModel> GetPager(string dbName, int PageIndex = 1)
        {
            int i = 0;
            List<DbTableModel> tList = new List<DbTableModel>();
            SqlDataReader drTb = null;

            string strSql = string.Format("Initial Catalog={1};{0}", ConnStr, dbName);
            helper.ExecuteReader(strSql, StaticConstant.TableSql, out drTb, () =>
            {
                i++;
                tList.Add(new DbTableModel() { Id = i, Database = dbName, Table = drTb[0].ToString() });
            });
            return new PageModel<DbTableModel>() { PageIndex = PageIndex, PageCount = tList.Count % 10 == 0 ? tList.Count / 10 : (tList.Count / 10) + 1, Rows = tList.Skip((PageIndex - 1) * 10).Take(10).ToList() };
        } 
        #endregion

     
    }
}
